/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import bean.LoginActionFormBean;
import dao.ActivityHistoryCrud;
import dao.ProviderDao;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.LogFile;
import model.OSDetector;
import model.ScheduledTaskLinux;
import model.ScheduledTaskWindows;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import pojo.Activities;
import pojo.ActivityHistory;
import pojo.Provider;

/**
 *
 * @author Mostafa
 */
//this class resposible for All Schedule tasks operations...
public class STOperations extends DispatchAction {
    /* forward name="success" path="" */

    private final static String SUCCESS = "display";

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    //this method for disabling schedule task...
    public ActionForward disable(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int taskid = Integer.parseInt(request.getParameter("taskid"));
        LoginActionFormBean loginbean = (LoginActionFormBean) request.getSession().getAttribute("LoginActionFormBean");
        Provider pro = new ProviderDao(getDataSource(request, "MyDS").getConnection()).getById(taskid);
        boolean disableresult = false;
        if (OSDetector.isWindows()) {
            disableresult = new ScheduledTaskWindows(getDataSource(request, "MyDS").getConnection()).disable(taskid);
        }
        if (OSDetector.isUnix()) {
            disableresult = new ScheduledTaskLinux(getDataSource(request, "MyDS").getConnection()).disable(taskid);
        }
        boolean logresult = activityHistory("Disable Schedule Task", request, taskid);
        if (disableresult == true) {
            LogFile.log(loginbean.getUserName(), "changed status of schedule task of the provider " + pro.getName() + "to be disabled");
        } else {
            LogFile.log("System", "failed to disable schedule task of the provider " + pro.getName());
        }
        if (logresult == true) {
            LogFile.log("System", "Logged the operation of disabling the schedule task of the provider " + pro.getName());
        } else {
            LogFile.log("System", "failed to log the operation of disabling the schedule task of the provider " + pro.getName());
        }
        return mapping.findForward(SUCCESS);
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    //this method for changing schedule task status to be enabled...
    public ActionForward enable(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int taskid = Integer.parseInt(request.getParameter("taskid"));
        LoginActionFormBean loginbean = (LoginActionFormBean) request.getSession().getAttribute("LoginActionFormBean");
        Provider pro = new ProviderDao(getDataSource(request, "MyDS").getConnection()).getById(taskid);
        boolean result = false;
        if (OSDetector.isWindows()) {
            result = new ScheduledTaskWindows(getDataSource(request, "MyDS").getConnection()).enable(taskid);
        }
        if (OSDetector.isUnix()) {
            result = new ScheduledTaskLinux(getDataSource(request, "MyDS").getConnection()).enable(taskid);
        }
        if (result == true) {
            LogFile.log(loginbean.getUserName(), "changed status of schedule task of the provider " + pro.getName() + "to be enabled");
        } else {
            LogFile.log("System", "failed to enable schedule task of the provider " + pro.getName());
        }
        boolean logresult = activityHistory("Enable Schedule Task", request, taskid);
        if (logresult == true) {
            LogFile.log("System", "Logged the operation of enabling the schedule task of the provider " + pro.getName());
        } else {
            LogFile.log("System", "failed to log the operation of enabling the schedule task of the provider " + pro.getName());
        }
        return mapping.findForward(SUCCESS);
    }

    //this method to start the specified schedule task now 
    public ActionForward start(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int taskid = Integer.parseInt(request.getParameter("taskid"));
        LoginActionFormBean loginbean = (LoginActionFormBean) request.getSession().getAttribute("LoginActionFormBean");
        Provider pro = new ProviderDao(getDataSource(request, "MyDS").getConnection()).getById(taskid);
        boolean startresult = false;
        if (OSDetector.isWindows()) {
            startresult = new ScheduledTaskWindows(getDataSource(request, "MyDS").getConnection()).start(taskid);
        }
        if (OSDetector.isUnix()) {
            startresult = new ScheduledTaskLinux(getDataSource(request, "MyDS").getConnection()).start(taskid);
        }
        boolean logresult = activityHistory("Start Schedule Task", request, taskid);
        if (startresult == true) {
            LogFile.log(loginbean.getUserName(), "started schedule task of the provider " + pro.getName() + " manaually at " + new Date());
        } else {
            LogFile.log("System", "failed to Start schedule task of the provider " + pro.getName() + " manually at " + new Date());
        }
        if (logresult == true) {
            LogFile.log("System", "Logged the operation of Starting the schedule task of the provider " + pro.getName() + " manually at " + new Date());
        } else {
            LogFile.log("System", "failed to log the operation of starting the schedule task of the provider " + pro.getName() + " manually at " + new Date());
        }
        return mapping.findForward(SUCCESS);
    }

//    //this method to end running schedule task
//    public ActionForward end(ActionMapping mapping, ActionForm form,
//            HttpServletRequest request, HttpServletResponse response)
//            throws Exception {
//                int taskid = Integer.parseInt(request.getParameter("taskid"));
//                LoginActionFormBean loginbean = (LoginActionFormBean) request.getSession().getAttribute("LoginActionFormBean");
//                Provider pro = new ProviderDao(getDataSource(request, "MyDS").getConnection()).getById(taskid);
//                boolean endresult=false;
//                if(OSDetector.isWindows()){
//                    endresult = new ScheduledTaskWindows(getDataSource(request, "MyDS").getConnection()).end(taskid);
//                }
//                if(OSDetector.isUnix())
//                {
//                    endresult = new ScheduledTaskLinux(getDataSource(request, "MyDS").getConnection()).end(taskid);
//                }
//                boolean logresult = activityHistory("Stop Schedule Task", request, taskid);
//        if (endresult == true) {
//            LogFile.log(loginbean.getUserName(), "stopped schedule task of the provider " + pro.getName() + " manaually at " + new Date());
//        } else {
//            LogFile.log(" System ", " failed to end schedule task of the provider " + pro.getName() + " manually at " + new Date());
//        }
//        if (logresult == true) {
//            LogFile.log(" System ", " Logged the operation of ending the schedule task of the provider " + pro.getName() + " manually at " + new Date());
//        } else {
//            LogFile.log(" System ", " failed to log the operation of ending the schedule task of the provider " + pro.getName() + " manually at " + new Date());
//        }
//        return mapping.findForward(SUCCESS);
//    }

//this method  to delete schedule task ...
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int taskid = Integer.parseInt(request.getParameter("taskid"));
        String provName=request.getParameter("name");
        LoginActionFormBean loginbean = (LoginActionFormBean) request.getSession().getAttribute("LoginActionFormBean");
        ProviderDao prodao = new ProviderDao(getDataSource(request, "MyDS").getConnection());
        Provider pro=prodao.getById(taskid);
        boolean logresult = activityHistory("Delete Schedule Task", request, taskid);
        if(OSDetector.isWindows())
        {
            new ScheduledTaskWindows(getDataSource(request, "MyDS").getConnection()).delete(taskid);
        }
        if(OSDetector.isUnix())
        {
            System.out.println("Hello Linux Server(Delete Method)");
            new ScheduledTaskLinux(getDataSource(request, "MyDS").getConnection()).delete(taskid);
        }
        boolean deleteresult=prodao.deleteById(taskid);
        boolean dropResult=prodao.dropProviderTable(provName+"_dim");
        if (deleteresult == true && dropResult == true) {
            LogFile.log(loginbean.getUserName(), "deleted schedule task of the provider " + pro.getName());
        } else {
            LogFile.log("System", "failed to delete schedule task of the provider " + pro.getName());
        }
        if (logresult == true) {
            LogFile.log("System", "Logged the operation of deleting the schedule task of the provider " + pro.getName());
        } else {
            LogFile.log("System", "failed to log the operation of deleting the schedule task of the provider " + pro.getName());
        }
        return mapping.findForward(SUCCESS);
    }

    //Add Activity to Activity History
    public boolean activityHistory(String activityName, HttpServletRequest request, int taskid) {
        boolean check = false;
        try {
            LoginActionFormBean loginbean = (LoginActionFormBean) request.getSession().getAttribute("LoginActionFormBean");
            ActivityHistory log = new ActivityHistory();
            log.setAdminName(loginbean.getUserName());
            ProviderDao pdao = new ProviderDao(getDataSource(request, "MyDS").getConnection());
            Provider pro = pdao.getById(taskid);
            log.setProviderName(pro.getName());
            log.setStatus("Success");
            log.setLiveServer(null);
            ActivityHistoryCrud ahc = new ActivityHistoryCrud(getDataSource(request, "MyDS").getConnection());
            Activities act = ahc.getByName(activityName);
            log.setActivities(act);
            log.setActivityName(act.getName());
            check = ahc.add(log);
        } catch (SQLException ex) {
            Logger.getLogger(STOperations.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following exception " + ex.getMessage());
            check = false;
        }
        return check;
    }
}
